package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.Dept;
import cn.kgc.springboot.vue.exception.BusinessException;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.service.DeptService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 课工场
 * @date 2024/12/21
 * @description
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Resource
    private DeptService deptService;

    // 查询部门列表
    @GetMapping("list")
    public ResponseResult deptList(){
        List<Dept> list = deptService.list();
        return  ResponseResult.success().data("deptList",list);
    }


    @PostMapping("page")
    public ResponseResult deptPage(@RequestBody QueryCondition<Dept> queryCondition){

         return    deptService.deptPage(queryCondition);
    }

    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateDept(@RequestBody Dept dept){

        try {
            deptService.saveOrUpdate(dept);
        }catch (DuplicateKeyException  e){
            throw  new BusinessException("部门名称重复");
        }

        return ResponseResult.success();
    }

    @DeleteMapping("delete/{id}")
    public  ResponseResult  deleteDept(@PathVariable Integer id){

      return   deptService.deleteDept(id);

    }
}
